package com.ckd.flyingtrip.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.MyfragmentViewpageAdapter;
import com.ckd.flyingtrip.fragment.MyWallet_Mingxi;
import com.ckd.flyingtrip.fragment.MyWallet_Jifen;
import com.ckd.flyingtrip.fragment.MyWallet_Qianbao;
import com.ckd.flyingtrip.fragment.MyWallet_Yajin;
import com.ckd.flyingtrip.fragment.MyWallet_Youhuijuan;
import com.ckd.flyingtrip.utils.Tools;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*
* 钱包
* */
    public class MyWallet extends BaseActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.cd_title_img)
    ImageView cdTitleImg;
    @BindView(R.id.cd_close)
    TextView cdClose;
    @BindView(R.id.cd_title_btn)
    TextView cdTitleBtn;
    @BindView(R.id.btn_yue)
    TextView btnYue;
    @BindView(R.id.btn_youhuijia)
    TextView btnYouhuijia;
    @BindView(R.id.btn_jifen)
    TextView btnJifen;
    @BindView(R.id.btn_yajin)
    TextView btnYajin;
    @BindView(R.id.btn_mingxi)
    TextView btnMingxi;
    @BindView(R.id.viewpage)
    ViewPager myvirwpager;
    // 指示标签的横坐标
    private float cursorX = 0;
    // 定义获取所有按钮的宽度数组
    private int[] WidrhArgs;
    // 定义所有标题按钮的数组
    private TextView[] ButtonArgs;
    // fragment的集合
    private ArrayList<Fragment> list;
    // viewpage适配器
    private MyfragmentViewpageAdapter adapter;
    FragmentManager fm = getSupportFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mywallet);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Tools.title_info(this, "钱包");

        ButtonArgs = new TextView[] { btnYue, btnYouhuijia,btnJifen, btnYajin,btnMingxi};
        // 将fragment放进集合，并初始化适配器
        list = new ArrayList<Fragment>();
//        余额 优惠券 积分 押金 明细
        list.add(new MyWallet_Qianbao());
        list.add(new MyWallet_Youhuijuan());
        list.add(new MyWallet_Jifen());
        list.add(new MyWallet_Yajin(this));
        list.add(new MyWallet_Mingxi());
        adapter = new MyfragmentViewpageAdapter(fm,
                list);
        myvirwpager.setAdapter(adapter);
        // viewpage监听事件，重写onPageSelected()方法，实现左右滑动页面
        myvirwpager.setOnPageChangeListener(this);
        colorInit();
    }

    @SuppressLint("ResourceAsColor")
    private void colorInit(){
        for (int i=0;i<ButtonArgs.length;i++){
            ButtonArgs[i].setTextColor(Color.parseColor("#454C52"));
        }
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(positionOffsetPixels==0){
            colorInit();
            ButtonArgs[position].setTextColor(Color.parseColor("#FD5B0E"));
        }
    }


    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (WidrhArgs == null) {
            WidrhArgs = new int[] { btnYue.getWidth(), btnYouhuijia.getWidth(),btnJifen.getWidth(), btnYajin.getWidth(),btnMingxi.getWidth()};;
        }
    }


    // 指示器的跳转，传入当前所处的页面的下标
    public void cursorAnim(int curItem) {
        // 每次调用，就将指示器的横坐标设置为0，即开始的位置
        cursorX = 0;
        colorInit();
        ButtonArgs[curItem].setTextColor(Color.parseColor("#FD5B0E"));
    }

    @OnClick({R.id.btn_yue, R.id.btn_youhuijia, R.id.btn_jifen, R.id.btn_yajin, R.id.btn_mingxi})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_yue:
                myvirwpager.setCurrentItem(0);
                cursorAnim(0);
                break;
            case R.id.btn_youhuijia:
                myvirwpager.setCurrentItem(1);
                cursorAnim(1);
                break;
            case R.id.btn_jifen:
                myvirwpager.setCurrentItem(2);
                cursorAnim(2);
                break;
            case R.id.btn_yajin:
                myvirwpager.setCurrentItem(3);
                cursorAnim(3);
                break;
            case R.id.btn_mingxi:
                myvirwpager.setCurrentItem(4);
                cursorAnim(4);
                break;
        }
    }
}
